package com.cy.singleton;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
/**
 * <br>@class: HungarySingleton
 * <br>@description: 饿汉单例
 * <br>@author: cy
 * <br>@date: 2020-03-28 22:07:22
 * <br>@version: 1.0
 * <br>
 * <br>@since: (版本) 作者 时间 注释
 */
public class HungarySingleton {

    private static HungarySingleton hungarySingleton = new HungarySingleton();

    private HungarySingleton() {
        //饿汉模式...
       if(HungarySingleton.hungarySingleton!=null){
           //需要防止反射
           throw new RuntimeException("反射多例错误");
       }
    }

    public static HungarySingleton getInstance() {
        //类初始化时候 实例化
        return hungarySingleton;
    }
    public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        for (int i = 0; i <10 ; i++) {
            new Thread(()->{
                HungarySingleton obj = HungarySingleton.getInstance();
                System.out.println(obj);
            }).start();
        }
        Constructor<HungarySingleton> declaredConstructor = HungarySingleton.class.getDeclaredConstructor();
        declaredConstructor.setAccessible(true);
        HungarySingleton h = declaredConstructor.newInstance();
        System.out.println("sssssssss"+h);
    }
}
